package com.anysoft.view.picker;

import android.content.Context;
import android.view.View;

import com.anysoft.MentorConst;
import com.bigkoo.pickerview.adapter.NumericWheelAdapter;
import com.bigkoo.pickerview.lib.WheelView;
import com.bigkoo.pickerview.listener.OnItemSelectedListener;
import com.anysoft.utils.MentorLogger;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;


public class IcbcEndWheelTime {
    public static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private View view;
    private WheelView wv_year;
    private WheelView wv_month;
    private WheelView wv_day;
    private WheelView wv_hours;
    private WheelView wv_mins;

    private MentorConst.DateType type;
    public static final int DEFULT_START_YEAR = 2017;
    public static final int DEFULT_END_YEAR = 3000;
    private int startYear = DEFULT_START_YEAR;
    private int endYear = DEFULT_END_YEAR;

    private int currentYear;
    private int currentMonth;
    private int currentDay;

    private int currentSelectedYear = 0;
    private int currentSelectedMonth = 0;
    private boolean isFirstInit = true;

    public IcbcEndWheelTime(View view) {
        super();
        this.view = view;
        type = MentorConst.DateType.ALL;
        setView(view);
    }

    public IcbcEndWheelTime(View view, MentorConst.DateType type) {
        super();
        this.view = view;
        this.type = type;
        setView(view);
    }

    public void setPicker(int year, int month, int day) {
        this.setPicker(year, month, day, 0, 0);
    }

    public void setPicker(int year, int month, int day, int h, int m) {
        // 添加大小月月份并将其转换为list,方便之后的判断
        String[] months_big = {"1", "3", "5", "7", "8", "10", "12"};
        String[] months_little = {"4", "6", "9", "11"};

        final List<String> list_big = Arrays.asList(months_big);
        final List<String> list_little = Arrays.asList(months_little);

        Context context = view.getContext();
        // 年
        wv_year = (WheelView) view.findViewById(com.bigkoo.pickerview.R.id.year);
        wv_year.setAdapter(new NumericWheelAdapter(1, 2017));// 设置"年"的显示数据
//        wv_year.setLabel(context.getString(com.bigkoo.pickerview.R.string.pickerview_year));// 添加文字
        wv_year.setCurrentItem(2017);// 初始化时显示的数据

        currentSelectedYear = year;

        // 月
        wv_month = (WheelView) view.findViewById(com.bigkoo.pickerview.R.id.month);
        MentorLogger.Log("timePicker---", currentYear + "---" + currentMonth + "---" + currentDay);
        if (year == currentYear) {
            wv_month.setAdapter(new NumericWheelAdapter(1, currentMonth));
        } else {
            wv_month.setAdapter(new NumericWheelAdapter(1, 12));
        }
//        wv_month.setLabel(context.getString(com.bigkoo.pickerview.R.string.pickerview_month));
        wv_month.setCurrentItem(currentMonth - 1);

        currentSelectedMonth = currentMonth;

        // 日
        wv_day = (WheelView) view.findViewById(com.bigkoo.pickerview.R.id.day);
        if (year == currentYear && month + 1 == currentMonth) {
            // 判断大小月及是否闰年,用来确定"日"的数据
            if (list_big.contains(String.valueOf(month + 1))) {
                wv_day.setAdapter(new NumericWheelAdapter(1, currentDay));
            } else if (list_little.contains(String.valueOf(month + 1))) {
                wv_day.setAdapter(new NumericWheelAdapter(1, currentDay));
            } else {
                // 闰年
                if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
                    wv_day.setAdapter(new NumericWheelAdapter(1, currentDay));
                else
                    wv_day.setAdapter(new NumericWheelAdapter(1, currentDay));
            }
        } else {
            // 判断大小月及是否闰年,用来确定"日"的数据
            if (list_big.contains(String.valueOf(month + 1))) {
                wv_day.setAdapter(new NumericWheelAdapter(1, 31));
            } else if (list_little.contains(String.valueOf(month + 1))) {
                wv_day.setAdapter(new NumericWheelAdapter(1, 30));
            } else {
                // 闰年
                if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
                    wv_day.setAdapter(new NumericWheelAdapter(1, 29));
                else
                    wv_day.setAdapter(new NumericWheelAdapter(1, 28));
            }
        }

//        wv_day.setLabel(context.getString(com.bigkoo.pickerview.R.string.pickerview_day));
        wv_day.setCurrentItem(currentDay - 1);


        wv_hours = (WheelView) view.findViewById(com.bigkoo.pickerview.R.id.hour);
        wv_hours.setAdapter(new NumericWheelAdapter(0, 23));
        wv_hours.setLabel(context.getString(com.bigkoo.pickerview.R.string.pickerview_hours));// 添加文字
        wv_hours.setCurrentItem(h);

        wv_mins = (WheelView) view.findViewById(com.bigkoo.pickerview.R.id.min);
        wv_mins.setAdapter(new NumericWheelAdapter(0, 59));
        wv_mins.setLabel(context.getString(com.bigkoo.pickerview.R.string.pickerview_minutes));// 添加文字
        wv_mins.setCurrentItem(m);

        // 添加"年"监听
        OnItemSelectedListener wheelListener_year = new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int year_num = index + 1;
                // 判断大小月及是否闰年,用来确定"日"的数据

                if (currentSelectedYear != year_num) {
                    if (!isFirstInit) {
                        currentSelectedMonth = -1;
                    } else {
                        isFirstInit = false;
                    }
                    if (currentYear == year_num) {
                        wv_month.setAdapter(new NumericWheelAdapter(1, currentMonth));

                        if (wv_month.getCurrentItem() + 1 > currentMonth) {
                            wv_month.setCurrentItem(currentMonth - 1);
                        }

                        if (wv_month.getCurrentItem() + 1 >= currentMonth) {
                            int maxItem;
                            if (list_big
                                    .contains(String.valueOf(wv_month.getCurrentItem() + 1))) {
                                wv_day.setAdapter(new NumericWheelAdapter(1, currentDay));
                                maxItem = 31;
                            } else if (list_little.contains(String.valueOf(wv_month
                                    .getCurrentItem() + 1))) {
                                wv_day.setAdapter(new NumericWheelAdapter(1, currentDay > 30 ? 30 : currentDay));
                                maxItem = 30;
                            } else {
                                if ((year_num % 4 == 0 && year_num % 100 != 0)
                                        || year_num % 400 == 0) {
                                    wv_day.setAdapter(new NumericWheelAdapter(1, currentDay > 29 ? 29 : currentDay));
                                    maxItem = 29;
                                } else {
                                    wv_day.setAdapter(new NumericWheelAdapter(1, currentDay > 28 ? 28 : currentDay));
                                    maxItem = 28;
                                }
                            }
                            //System.out.println("wv_day.getCurrentItem()--" + wv_day.getCurrentItem() + ",maxItem---"
//                                    + maxItem);
                            if (wv_day.getCurrentItem() + 1 > currentDay) {
                                wv_day.setCurrentItem(currentDay - 1);
                            }
                        } else {
                            int maxItem;
                            if (list_big
                                    .contains(String.valueOf(wv_month.getCurrentItem() + 1))) {
                                wv_day.setAdapter(new NumericWheelAdapter(1, 31));
                                maxItem = 31;
                            } else if (list_little.contains(String.valueOf(wv_month
                                    .getCurrentItem() + 1))) {
                                wv_day.setAdapter(new NumericWheelAdapter(1, 30));
                                maxItem = 30;
                            } else {
                                if ((year_num % 4 == 0 && year_num % 100 != 0)
                                        || year_num % 400 == 0) {
                                    wv_day.setAdapter(new NumericWheelAdapter(1, 29));
                                    maxItem = 29;
                                } else {
                                    wv_day.setAdapter(new NumericWheelAdapter(1, 28));
                                    maxItem = 28;
                                }
                            }
                            if (wv_day.getCurrentItem() > maxItem - 1) {
                                wv_day.setCurrentItem(maxItem - 1);
                            }
                        }

                        currentSelectedYear = year_num;
                    } else {
                        wv_month.setAdapter(new NumericWheelAdapter(1, 12));

                        if (currentSelectedYear == currentYear) {
                            //System.out.println("wv_month.getCurrentItem()1---" + wv_month.getCurrentItem() +
//                                    ",wv_month.getCurrentItem() + currentMonth - 1----"
//                                    + (wv_month.getCurrentItem() + currentMonth - 1));
                            wv_month.setCurrentItem(wv_month.getCurrentItem());
                            //System.out.println("wv_month.getCurrentItem()2---" + wv_month.getCurrentItem());

                            int maxItem;
                            if (list_big
                                    .contains(String.valueOf(wv_month.getCurrentItem() + currentMonth))) {
                                wv_day.setAdapter(new NumericWheelAdapter(1, 31));
                                maxItem = 31;
                            } else if (list_little.contains(String.valueOf(wv_month
                                    .getCurrentItem() + currentMonth))) {
                                wv_day.setAdapter(new NumericWheelAdapter(1, 30));
                                maxItem = 30;
                            } else {
                                if ((year_num % 4 == 0 && year_num % 100 != 0)
                                        || year_num % 400 == 0) {
                                    wv_day.setAdapter(new NumericWheelAdapter(1, 29));
                                    maxItem = 29;
                                } else {
                                    wv_day.setAdapter(new NumericWheelAdapter(1, 28));
                                    maxItem = 28;
                                }
                            }

                            if (wv_month.getCurrentItem() + currentMonth <= currentMonth) {

                                if (wv_day.getCurrentItem() > maxItem - 1) {
                                    wv_day.setCurrentItem(maxItem - 1);
                                }
                            } else {
                                if (wv_day.getCurrentItem() > currentDay - 1) {
                                    wv_day.setCurrentItem(currentDay - 1);
                                } else {

                                }
                            }
                        } else {
                            int maxItem;
                            if (list_big
                                    .contains(String.valueOf(wv_month.getCurrentItem() + 1))) {
                                wv_day.setAdapter(new NumericWheelAdapter(1, 31));
                                maxItem = 31;
                            } else if (list_little.contains(String.valueOf(wv_month
                                    .getCurrentItem() + 1))) {
                                wv_day.setAdapter(new NumericWheelAdapter(1, 30));
                                maxItem = 30;
                            } else {
                                if ((year_num % 4 == 0 && year_num % 100 != 0)
                                        || year_num % 400 == 0) {
                                    wv_day.setAdapter(new NumericWheelAdapter(1, 29));
                                    maxItem = 29;
                                } else {
                                    wv_day.setAdapter(new NumericWheelAdapter(1, 28));
                                    maxItem = 28;
                                }
                            }

                            if (wv_day.getCurrentItem() > maxItem - 1) {
                                wv_day.setCurrentItem(maxItem - 1);
                            }
                        }

                        currentSelectedYear = year_num;
                    }
                }
            }
        };
        // 添加"月"监听
        OnItemSelectedListener wheelListener_month = new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int month_num = index + 1;


                if (wv_year.getCurrentItem() == currentYear - 1) {
                    //System.out.println(111 + "---currentSelectedMonth:" + currentSelectedMonth
//                            + ",index + currentMonth---" + (index + currentMonth));
                    if (currentSelectedMonth != index + currentMonth) {
                        //System.out.println("index---" + index);
                        if (index > currentMonth - 2) {
                            // 判断大小月及是否闰年,用来确定"日"的数据
                            //System.out.println("wv_month.getCurrentItem() + currentMonth1---" + (wv_month.getCurrentItem() + currentMonth));
                            if (list_big.contains(String.valueOf(wv_month.getCurrentItem() + currentMonth))) {
                                wv_day.setAdapter(new NumericWheelAdapter(1, currentDay));
                            } else if (list_little.contains(String.valueOf(wv_month.getCurrentItem() + currentMonth))) {
                                wv_day.setAdapter(new NumericWheelAdapter(1, currentDay > 30 ? 30 : currentDay));
                            } else {
                                if (((wv_year.getCurrentItem() + startYear) % 4 == 0 && (wv_year
                                        .getCurrentItem() + startYear) % 100 != 0)
                                        || (wv_year.getCurrentItem() + startYear) % 400 == 0) {
                                    wv_day.setAdapter(new NumericWheelAdapter(1, currentDay > 29 ? 29 : currentDay));
                                } else {
                                    wv_day.setAdapter(new NumericWheelAdapter(1, currentDay > 28 ? 28 : currentDay));
                                }
                            }


                            if (wv_day.getCurrentItem() >= currentDay - 1) {
                                wv_day.setCurrentItem(currentDay - 1);
                            } else {
                                wv_day.setCurrentItem(wv_day.getCurrentItem());
                            }
                        } else {
                            int maxItem;
                            // 判断大小月及是否闰年,用来确定"日"的数据
                            //System.out.println("wv_month.getCurrentItem() + currentMonth2---" + (wv_month.getCurrentItem() + currentMonth));
                            if (list_big.contains(String.valueOf(wv_month.getCurrentItem() + 1))) {
                                wv_day.setAdapter(new NumericWheelAdapter(1, 31));
                                maxItem = 31;
                            } else if (list_little.contains(String.valueOf(wv_month.getCurrentItem() + 1))) {
                                wv_day.setAdapter(new NumericWheelAdapter(1, 30));
                                maxItem = 30;
                            } else {
                                if (((wv_year.getCurrentItem() + startYear) % 4 == 0 && (wv_year
                                        .getCurrentItem() + startYear) % 100 != 0)
                                        || (wv_year.getCurrentItem() + startYear) % 400 == 0) {
                                    wv_day.setAdapter(new NumericWheelAdapter(1, 29));
                                    maxItem = 29;
                                } else {
                                    wv_day.setAdapter(new NumericWheelAdapter(1, 28));
                                    maxItem = 28;
                                }
                            }
                            if (currentSelectedMonth != currentMonth) {
                                if (wv_day.getCurrentItem() > maxItem - 1) {
                                    wv_day.setCurrentItem(maxItem - 1);
                                }
                            } else {
                                if (wv_day.getCurrentItem() > currentDay - 1) {
                                    wv_day.setCurrentItem(currentDay - 1);
                                }
                            }
                        }
                    }

                    currentSelectedMonth = wv_month.getCurrentItem();
                } else {
                    if (currentSelectedMonth != month_num) {
                        int maxItem;
                        // 判断大小月及是否闰年,用来确定"日"的数据
                        if (list_big.contains(String.valueOf(month_num))) {
                            wv_day.setAdapter(new NumericWheelAdapter(1, 31));
                            maxItem = 31;
                        } else if (list_little.contains(String.valueOf(month_num))) {
                            wv_day.setAdapter(new NumericWheelAdapter(1, 30));
                            maxItem = 30;
                        } else {
                            if (((wv_year.getCurrentItem() + startYear) % 4 == 0 && (wv_year
                                    .getCurrentItem() + startYear) % 100 != 0)
                                    || (wv_year.getCurrentItem() + startYear) % 400 == 0) {
                                wv_day.setAdapter(new NumericWheelAdapter(1, 29));
                                maxItem = 29;
                            } else {
                                wv_day.setAdapter(new NumericWheelAdapter(1, 28));
                                maxItem = 28;
                            }
                        }
                        if (wv_day.getCurrentItem() > maxItem - 1) {
                            wv_day.setCurrentItem(maxItem - 1);
                        }

                        currentSelectedMonth = month_num;
                    }
                }
            }
        };
        wv_year.setOnItemSelectedListener(wheelListener_year);
        wv_month.setOnItemSelectedListener(wheelListener_month);

        // 根据屏幕密度来指定选择器字体的大小(不同屏幕可能不同)
        int textSize = 6;
        switch (type) {
            case ALL:
                textSize = textSize * 3;
                break;
            case YEAR_MONTH_DAY:
                textSize = textSize * 4;
                wv_hours.setVisibility(View.GONE);
                wv_mins.setVisibility(View.GONE);
                break;
            case HOURS_MINS:
                textSize = textSize * 4;
                wv_year.setVisibility(View.GONE);
                wv_month.setVisibility(View.GONE);
                wv_day.setVisibility(View.GONE);
                break;
            case MONTH_DAY_HOUR_MIN:
                textSize = textSize * 3;
                wv_year.setVisibility(View.GONE);
                break;
            case YEAR_MONTH:
                textSize = textSize * 4;
                wv_day.setVisibility(View.GONE);
                wv_hours.setVisibility(View.GONE);
                wv_mins.setVisibility(View.GONE);
        }
        wv_day.setTextSize(textSize);
        wv_month.setTextSize(textSize);
        wv_year.setTextSize(textSize);
        wv_hours.setTextSize(textSize);
        wv_mins.setTextSize(textSize);

    }

    /**
     * 设置是否循环滚动
     *
     * @param cyclic
     */
    public void setCyclic(boolean cyclic) {
        wv_year.setCyclic(cyclic);
        wv_month.setCyclic(cyclic);
        wv_day.setCyclic(cyclic);
        wv_hours.setCyclic(cyclic);
        wv_mins.setCyclic(cyclic);
    }

    public String getTime() {
        StringBuffer sb = new StringBuffer();
        //System.out.println("wv_year.getCurrentItem()---" + wv_year.getCurrentItem() +
//                ",wv_month.getCurrentItem()---" + wv_month.getCurrentItem());
        sb.append((wv_year.getCurrentItem() + 1)).append("-")
                .append((wv_month.getCurrentItem() + 1)).append("-")
                .append((wv_day.getCurrentItem() + 1)).append(" ")
                .append(wv_hours.getCurrentItem()).append(":")
                .append(wv_mins.getCurrentItem());
        return sb.toString();
    }

    public View getView() {
        return view;
    }

    public void setView(View view) {
        this.view = view;
    }

    public int getStartYear() {
        return startYear;
    }

    public void setStartYear(int startYear) {
        this.startYear = startYear;
    }

    public int getEndYear() {
        return endYear;
    }

    public void setEndYear(int endYear) {
        this.endYear = endYear;
    }

    public int getCurrentYear() {
        return currentYear;
    }

    public void setCurrentYear(int currentYear) {
        this.currentYear = currentYear;
    }

    public int getCurrentMonth() {
        return currentMonth;
    }

    public void setCurrentMonth(int currentMonth) {
        this.currentMonth = currentMonth;
    }

    public int getCurrentDay() {
        return currentDay;
    }

    public void setCurrentDay(int currentDay) {
        this.currentDay = currentDay;
    }
}
